export const mutations = {
    //点击歌曲时触发的事件
    upDateSong(state, data) {
        state.song = data.music
        let music = data.music
        var result = state.list.findIndex(item => {
            return music.id === item.id
        })
        if (result == -1) {
            var newsong = {
                name: music.songName,
                author: music.author,
                id: music.id,
                picUrl: music.picUrl
            }
            state.list.push(newsong);
            state.index = state.list.length - 1;
        } else {
            state.index = result
        }
        var audio = document.getElementById("myaudio");
        if (data.aotoplay) {
            audio.setAttribute("autoplay", "autoplay");
        }
        //将歌曲列表存储到本地
        window.localStorage.setItem("musicState", JSON.stringify({ list: state.list, index: state.index }), new Date().getDate + 20);
    },
    // 更新播放状态，歌曲是否可以播放  歌曲名字由  正在加载中--->歌曲真正的名称
    upDateStatus(state, newName) {
        state.song.name = newName
    },
    // 删除歌曲列表中的歌曲
    delete(state, index) {
        state.list.splice(index, 1);
        if (state.list == 0) {
            state.index = 0;
        } else if (index == state.index) {
            // console.log(index, state.index)
            if (index == state.list.length - 1) {
                state.index = 0;
            } else {
                state.index = index;
            }
            var item = state.list[state.index]
            this.dispatch("getSong", { id: item.id, autoplay: true })
        }

        window.localStorage.setItem("musicState", JSON.stringify({ list: state.list, index: state.index }), new Date().getDate + 20);
    },
    // 点击播放全部时触发的事件
    addlist(state, list) {
        state.index = state.list.length;
        var newlist = list.map(item => {
            return {
                name: item.name,
                author: item.ar[0].name,
                id: item.id,
                picUrl: item.al.picUrl
            }
        })
        state.list.push(...newlist);
        var item = state.list[state.index]
        this.dispatch("getSong", { id: item.id, autoPlay: true })
        window.localStorage.setItem("musicState", JSON.stringify({ list: state.list, index: state.index }), new Date().getDate + 20);
    },
    // 更新index
    upDateIndex(state, index) {
        if (state.index == index) return
        state.index = index
        var item = state.list[state.index]
        this.dispatch("getSong", { id: item.id, autoPlay: true })
        window.localStorage.setItem("musicState", JSON.stringify({ list: state.list, index: state.index }), new Date().getDate + 20);
    },
    // 下一首
    next(state) {
        let { model } = arguments[1]
        if (model % 3 == 1 || model % 3 == 2) {
            if (state.index == state.list.length - 1) {
                state.index = 0
            } else {
                state.index++
            }
        } else {
            state.index = Math.ceil(Math.random() * state.list.length)
        }

        var item = state.list[state.index]
        this.dispatch("getSong", { id: item.id, autoPlay: true })
        window.localStorage.setItem("musicState", JSON.stringify({ list: state.list, index: state.index }), new Date().getDate + 20);
    },
    //上一首
    prev(state) {
        let { model } = arguments[1]
        if (model % 3 == 1 || model % 3 == 2) {
            if (state.index == 0) {
                state.index = state.list.length - 1
            } else {
                state.index--
            }
        } else {
            state.index = Math.ceil(Math.random() * state.list.length)
        }
        var item = state.list[state.index]
        this.dispatch("getSong", { id: item.id, autoPlay: true })
        window.localStorage.setItem("musicState", JSON.stringify({ list: state.list, index: state.index }), new Date().getDate + 20);
    },
    // 初始化从本地读取缓存触发的事件
    pushlist(state, oldstate) {
        state.list = oldstate.list;
        state.index = oldstate.index;
        let item = state.list[state.index]
        this.dispatch("getSong", { id: item.id, autoPlay: false })
    },
    // 存入未读评论
    pushCom(state, arr) {
        state.unreadComments = arr
    },
    // 更新登陆状态
    upDateStaus(state, boolean) {
        state.isLogin = boolean
    },
    // 更新admin的信息
    upDateAdmin(state, admin) {
        state.admin = admin
    }
}